#!/bin/bash
#
#    am-tintedit: a arcolinux tint2 config file editor
#    Copyright (C) 2015 damo    <damo@bunsenlabs.org>
#    Borrowed with love by arcolinux.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
########################################################################
#
#   Tint2 files must be in $TINT2PATH
#
#   Checkmarked tint2s will be opened in the text editor
#   Multiple tint2s can be chosen
#
########################################################################

TINT2PATH="$HOME/.config/tint2"

HELP="    am-tint2edit a script to edit selected tint2 config files

Options:
    -h --help   show this message

No other options are supported.

Tint2 files must be in $TINT2PATH
Checkmarked tint2s will be opened in the text editor.
Multiple tint2s can be chosen.
"

### DIALOG VARIABLES
DLGDEC="yad --center  --borders=20 --width=400 --height=400 "
TITLE="ArcoLinux Tint2 Edit"
WINICON="--window-icon=distributor-logo-arcolinux"
OK="--button=yad-ok:0"
CANCEL="--button=yad-cancel:1"

########## FUNCTIONS ###################################################

fillArrays(){
    num="$1"
    tintsPath[$num]="$2"   # full filepath to tint2
    tintsArr[$num]="$3"    # displayed name: "directory/tintfile"
}

findTint(){
# search dirs for tint2 config files
    num=0
    for x in $(find $TINT2PATH -type f );do
        # check if likely tint2 config file
        pm=$(grep  "panel_monitor" "$x")
        if [[ $pm ]];then
            TINT2=$( echo $x | awk -F"/" '{print $(NF-1)"/"$NF}')
            fillArrays $num $x $TINT2
            num=$(($num+1))
        fi
    done
}
######## END FUNCTIONS #################################################

# look for a help option somewhere
for i in "$@"
do
    case "$i" in
    -h|--help)
        echo "$HELP"
        exit 0
        ;;
    esac
done

# get tint2 directories, add to array
findTint

# loop through arrays, and build msg text for yad dialog
unset LISTINT
for ((j=0; j<${#tintsArr[*]}; j++));do
    LISTINT="$LISTINT FALSE ${tintsArr[j]} "
done
## Populate dialog from array, get return value(s)
RET=$($DLGDEC $WINICON --list --title="$TITLE" \
    --text="Select Tint2s to edit from the list\nMultiple Tint2s can be chosen\n" \
    --checklist \
    --column="Select:CHK" --column="Tint2 Name:TXT" $LISTINT --separator=":" \
    $OK $CANCEL \
    )

if (( $? == 1 )); then # cancel button pressed
    exit 0
else
    i=0
    OIFS=$IFS   # save Internal Field Separator
    IFS=":"     # separator is ":" in returned choices
    for name in $RET; do
        retTint[$i]="$name"
        i=$(($i+1))
    done
    IFS=$OIFS   # reset IFS back to default

    # Find the chosen tint2s and edit them
    for name in ${retTint[*]};do       # loop through checkmarked tint2 names
        for ((j=0; j<${#tintsPath[*]}; j++));do  # traverse through elements
            for f in ${tintsPath[j]};do    # compare with choice from dialog
                display=$( echo "$f" | awk -F"/" '{print $(NF-1)"/"$NF}')
                if [[ $display = $name ]];then
                    geany "$f"
                fi
            done
        done
    done
fi

exit 0
